/**
 * 拖放管理器
 */
var DragAndDropManager = cc.Class({
    extends: cc.Component,

    properties: {
        draggingLayer:{
            default:null,
            type:cc.Node,
            tooltip:'用于拖拽时的层级'
        },
    },

    statics: {
        Singleton: null,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        if (!DragAndDropManager.Singleton) {
            // 创建监听器组
            this._dragListeners = {
            };
            this._dragListeners[DragAndDropManager.EventType.DRAG_START] = [];
            this._dragListeners[DragAndDropManager.EventType.DRAG_MOVE] = [];
            this._dragListeners[DragAndDropManager.EventType.DRAG_END] = [];
            // 记录单例
            DragAndDropManager.Singleton = this;
        }
        else {
            this.destroy();
        }
    },
    /**
     * 
     * @param {*} dragType 
     * @param {*} callback 
     * @param {*} priority 
     */
    addDragAndDropListener(dragType, callback, priority) {
        // 获取对应的监听器组
        var _listeners = this._dragListeners[dragType];
        if (_listeners) {
            var exists = false;
            _listeners.some(e => {
                if (e.callback === callback && e.priority === priority) {
                    exists = true;
                    return true;
                }
            });
            // 不重复添加
            if (exists === true)
                return;
            _listeners.push({ callback: callback, priority: priority });
            //
            _listeners.sort(this._sortListener);
        }
    },
    /**
     * 
     * @param {*} dragType 
     * @param {*} callback 
     * @param {*} priority 
     */
    removeDragAndDropListener(dragType, callback, priority) {
        var _listeners = this._dragListeners[dragType];
        if (_listeners) {
            _listeners.some((e, i) => {
                if (e.callback === callback && e.priority === priority) {
                    _listeners.splice(i, 1);
                    return true;
                }
            });
        }
    },
    /**
     * 发送拖拽事件
     * @param {cc.Node} dragTarget 
     */
    emitDrag(dragType, dragTarget, dragArea, touchLocation, touch) {
        var dragEvent = {
            type: dragType,
            target: dragTarget,
            area: dragArea,
            location: touchLocation,
            touch: touch,
            stop: false,
        };
        var _listeners = this._dragListeners[dragType];
        if (_listeners) {
            for (var i = 0; i < _listeners.length; ++i) {
                _listeners[i].callback(dragEvent);
                // 是否上一个处理终止事件继续传递
                if (dragEvent.stop === true)
                    break;
            }
        }
    },
    getDraggingLayer(){
        // 检测缓存的拖拽层的有效性
        this.draggingLayer && this.draggingLayer.isValid != true && (this.draggingLayer = null);
        return this.draggingLayer;
    },
    _sortListener(a, b) {
        return a.priority - b.priority;
    },
});

DragAndDropManager.EventType = {
    DRAG_START: 'dragStart',
    DRAG_MOVE: 'dragMove',
    DRAG_END: 'dragEnd',
    DROP_ENTER: 'dropEnter',
    DROP_HOVER: 'dropHover',
    DROP_EXIT: 'dropExit',
    DROP_END: 'dropEnd',
};

module.exports = DragAndDropManager;